package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.CommBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.DisplayUtil;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.ShadowDrawable;

import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: 社区主播头像adapter
 * @Description: java类作用描述
 * @Author: ljp
 * @CreateDate: 2018/9/4 14:11
 * @Version: 1.0
 */
public class AnchorIconAdapter extends BaseAdapter<CommBean.MapBean.RealTimeBean>{
    private Context context;
    private  List<CommBean.MapBean.RealTimeBean> tlist;
    int number = 0;
    Thread thread;
    private OnClick onClick;
    private String TAG = "AnchorIconAdapter";

    public void setOnClick(OnClick onClick) {
        this.onClick = onClick;
    }
    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public AnchorIconAdapter(Context context, List<CommBean.MapBean.RealTimeBean> tlist, int mLayout) {
        super(context, tlist, mLayout);
        this.context = context;
        this.tlist = tlist;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        ViewHolder1 viewHolder1 = new ViewHolder1(view);

        return viewHolder1;
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
       if(viewHolder instanceof ViewHolder1){
           ShadowDrawable.setShadowDrawable(((ViewHolder1) viewHolder).relativeSnow,
                   Color.parseColor("#ffffff"),
                   DisplayUtil.dp2px(context,4),
                   Color.parseColor("#24666666"), DisplayUtil.dp2px(context,5), 0, 0);
           //setImage(tlist.get(position).getAvatarUrl(),((ViewHolder1) viewHolder).imageIcon);//下载头像
           if (!tlist.get(position).equals(((ViewHolder1) viewHolder).circlePicture.getTag(R.id.circle_picture))) {
               // 加载图片
               Log.i(TAG, "onBindViewHolder: --OSS_HTTP-------"+OssConfig.OSS_HTTP+tlist.get(position).getAvatarUrl());
               ((ViewHolder1) viewHolder).circlePicture.setTag(R.id.circle_picture, tlist.get(position).getAvatarUrl());
               GlideLoadUtils.getInstance().glideLoad(context,OssConfig.OSS_HTTP+tlist.get(position).getAvatarUrl(),
                       ((ViewHolder1) viewHolder).circlePicture);
               //setImage(dynamic.getAvatarUrl(), headViewHolder.imageIcon);
           }
           ((ViewHolder1) viewHolder).circlePicture.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   onClick.onSetPictureClick(((ViewHolder1) viewHolder).circlePicture,position);
               }
           });
           if (tlist.get(position).getGender() == 1) {//判断性别  1男 2女
               ((ViewHolder1) viewHolder).imageSex.setImageResource(R.mipmap.boy);
               ((ViewHolder1) viewHolder).linearSexAge.setBackgroundResource(R.mipmap.back_boy);
           } else if (tlist.get(position).getGender() == 2) {
               ((ViewHolder1) viewHolder).imageSex.setImageResource(R.mipmap.girl);
               ((ViewHolder1) viewHolder).linearSexAge.setBackgroundResource(R.mipmap.back_girl);
           }
           ((ViewHolder1) viewHolder).tvNikeName.setText(tlist.get(position).getNickName());
           String substring = tlist.get(position).getBirthday().substring(0, 4);
           String time = Utils.getStringTime(new Date());
           int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
           ((ViewHolder1) viewHolder).tvAge.setText(age+"");
        }
    }
    /**
     * 布局1
     */
    class ViewHolder1 extends BaseViewHolder{
        @BindView(R.id.circle_picture)
        ImageView circlePicture;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.tv_nike_name)
        TextView tvNikeName;
        @BindView(R.id.tv_value)
        TextView tvValue;
        @BindView(R.id.tv_attention)
        TextView tvAttention;
        @BindView(R.id.relative_snow)
        RelativeLayout relativeSnow;
        public ViewHolder1(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public  interface OnClick{
        void onSetPictureClick(View view, int position);
    }
}
